<template>
  <div
    class="dictionary-page"
    v-loading="loading"
    element-loading-text="加载中..."
  >
    <div class="dictionary-title">
      <div class="dictionary-title-left">
        <el-form inline>
          <el-form-item style="margin-bottom: 0">
            <el-input
              v-model="searchForm.name"
              placeholder="用户级别名或类型"
            ></el-input>
          </el-form-item>
          <el-form-item style="margin-bottom: 0">
            <el-button type="primary" @click="searchFun">查询</el-button>
            <el-button @click="resetFun">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="dictionary-title-right">
        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">
          添加
        </el-button>
      </div>
    </div>
    <div class="dictionary-table">
      <el-table
        border
        stripe
        :data="tableData"
        :height="store.tableHeight"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'sub', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="用户级别名称" />
        <el-table-column prop="number" label="用户级别序号" />
        <el-table-column
          prop="order_amount"
          label="订单金额累计/最小升级金额"
          width="300"
        />
        <el-table-column prop="cover" label="图标" width="100" align="center">
          <template #default="{ row }">
            <el-image
              :preview-teleported="true"
              style="width: 50px; height: 50px"
              :src="imgSrc(row.logo)"
              :zoom-rate="1.2"
              :preview-src-list="[imgSrc(row.logo)]"
              :initial-index="4"
              fit="cover"
            />
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注说明" />
        <el-table-column prop="created_at" label="创建时间" />

        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="updateDeptFun('update', row)"
            >
              编辑
            </el-button>

            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <addDialog ref="addDialogRef" @refreshList="refreshList" />
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import { listApi, delApi } from "@/plugins/api/shopping/grade";
import addDialog from "./components/DialogGrade.vue";
const store = useCounterStore();
const router = useRouter();
let tableData = ref([]);
let loading = ref(false);
let searchForm = ref({});
function searchFun() {
  tableData.value = [];
  getData();
}
function resetFun() {
  searchForm.value = {};
  tableData.value = [];
  getData();
}
function getData() {
  loading.value = true;
  listApi(searchForm.value)
    .then((data) => {
      tableData.value = data;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let addDialogRef = ref(null);
function addDeptFun(type) {
  addDialogRef.value.initFun(type);
}
function updateDeptFun(type, row) {
  addDialogRef.value.initFun(type, row);
}
function refreshList() {
  getData();
}

function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
function imgSrc(path) {
  return import.meta.env.VITE_APP_BASE_API + path;
}
onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.dictionary-page {
  width: 100%;
  height: 100%;

  .dictionary-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .dictionary-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      img {
        vertical-align: middle;
      }

      span {
        vertical-align: middle;
        margin-left: 5px;
      }
    }
  }

  .dictionary-table {
    padding: 10px;
    box-sizing: border-box;
  }
}
</style>
